package translation;

import DL.*;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by xwj on 2017/11/17.
 */
public class convert3D {

    final static String[] fileNames={"coaster2","coaster","diving","drive","game","landscape","pacman","panel","ride","sport"};
    final static String csvPath = "J:\\360dataset\\sensory\\orientation"; // csv路径
    final static String yuvPath = "J:\\360dataset\\360video\\yuv\\"; // yuv路径
    final static int width=1280;
    final static int height=720;
    final static int outWidth=400;
    final static int outHeight=300;


    public static String findFileNamePrefix(String fileName){
        for(String filePrefix:fileNames){
            if(fileName.startsWith(filePrefix))
                return filePrefix;
        }
        return "";
    }

    public static File[] getCsvFileNames() {
        File f = new File(csvPath);
        if (!f.exists()) {
            System.out.println(csvPath + " not exists");
            return null;
        }

        File listFiles[] = f.listFiles();
        return listFiles;
    }

    public static String getYuvFileName(String name) {
        return yuvPath+name+".yuv";
    }

    public static void main(String[] args) {
        File[] listFiles=getCsvFileNames();
        List<SPos> sPosList;
        SPos sPosIn=new SPos();
        PrintWriter output=null;
        //BufferedOutputStream output=null;
        try {
            output= new PrintWriter(new BufferedOutputStream(new FileOutputStream(new File("output.csv"))));
        }catch (Exception e){
            System.out.println("文件打开错误！");
            return;
        }

        for(File csv:listFiles) {
            System.out.println("开始读取" + csv.getName());
            BufferedReader br = null;
            try {
                br = new BufferedReader(new FileReader(csv));
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
            sPosList=new ArrayList<SPos>(2048);
            try {
                String line = br.readLine();
                while ((line = br.readLine()) != null)  //读取到的内容给line变量
                {
                    String[] infos=line.split(",");
                    double x=Double.parseDouble(infos[1]);
                    double y=Double.parseDouble(infos[2]);
                    double z=Double.parseDouble(infos[3]);
                    sPosList.add(Map3DTo2D.map3DTo2D(new SPos(x,y,z),width,height));
                }
                System.out.println("读取完" + csv.getName());
            } catch (IOException e) {
                e.printStackTrace();
            }
            /*
            for(SPos sPos:sPosList){
                System.out.println(csv.getName()+"  --  "+sPos.x+" "+sPos.y+" "+sPos.z);
            }
            */
            String prefix=findFileNamePrefix(csv.getName());
            if(prefix.equals("")) continue;
            String yuvFile=yuvPath+prefix+".yuv";
            try {

                //读取file文件
                File file = new File(yuvFile);
                InputStream in = new FileInputStream(yuvFile);
                int yuvSize=width*height*3/2;
                byte[] bytes=new byte[yuvSize];
                for (int i = 0; i + 30 < sPosList.size(); i++) {
                    byte[] writeBytes=new byte[outHeight*outWidth];
                    SPos sPos = sPosList.get(i);//获得输入图片中心
                    int x = (int) sPos.x;
                    int y = (int) sPos.y;
                    //读入yuv第i帧
                    int byteNum=in.read(bytes);
                    //get all input point
                    int count=0;
                    for (int h = -outHeight/2; h < outHeight/2; h++) {
                        int yuvY = (y + h + height) % height;
                        for (int w = -outWidth/2; w < outWidth/2; w++) {
                            int yuvX = (x + w + width) % width;
                            //get input point
                            //output.print(bytes[yuvX+yuvY*width]+",");
                            writeBytes[count]=bytes[yuvX+yuvY*width];
                            count++;
                        }
                    }
                    //获得预测label
                    SPos sPosLabel = sPosList.get(i + 30);
                    int number=(int)((sPosLabel.x+0.0)/width*10)+((int)((sPosLabel.y+0.0)/height*10))*10;
                    //output.println(number+"");
                    //System.out.println("count="+count);

                    OutputStream yuvout = new FileOutputStream("./yuvout/"+csv.getName()+"."+i+"."+number+".yuv");
                    yuvout.write(writeBytes);
                    yuvout.close();
                }

                System.out.println(csv.getName()+" 文件处理完毕");
            }catch (Exception e){
                System.out.println("--------------------------------------------------------------");
                System.out.println(csv.getName()+"读取文件错误");
            }

            output.flush();

        }

    }






}
